package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @auther gwt
 * @date 2021/6/18 22:48
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;


    @Override
    public Result order(Map map) throws Exception {
        //验证是否预约天(根据日期查预约设置对象)
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //预约人满?
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //是否同天同手机同套餐
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if (member != null) {
            Integer id = member.getId();
            Integer setmealId = Integer.parseInt((String) map.get("setmealId"));
            Order order = new Order(id, date, setmealId);
            List<Order> list = orderDao.findByCondition(order);
            if (list != null && list.size() > 0) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else {
            //是否是会员
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);//自动完成会员注册
        }

        //预约人数加一
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);
        //预约人数加一
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if (map != null) {
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate", DateUtils.parseDate2String(orderDate));
        }
        return map;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String begin = null;
        String end = null;
        Date[] dateScope = queryPageBean.getQueryDate();
        if (dateScope != null && dateScope.length > 0) {
            begin=DateUtils.parseDate2String(dateScope[0]);
            end = DateUtils.parseDate2String(dateScope[1]);
            map.put("begin", begin);
            map.put("end", end);
        }
        map.put("queryString", queryPageBean.getQueryString());
        map.put("orderType", queryPageBean.getQueryOrderType());
        map.put("orderStatus", queryPageBean.getQueryOrderStatus());


        PageHelper.startPage(currentPage, pageSize);
        Page orderQuery = orderDao.findByQueryString(map);
        long total = orderQuery.getTotal();
        List<OrderManager> result = orderQuery.getResult();
        return new PageResult(total, result);

    }

    @Override
    public void add(OrderManager orderManager) throws Exception {
        Member member = new Member();
        member.setName(orderManager.getName());
        member.setPhoneNumber(orderManager.getPhoneNumber());
        memberDao.add(member);


        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderStatus(orderManager.getOrderStatus());
        order.setOrderType(orderManager.getOrderType());
        order.setSetmealId((orderManager.getSetmealId()));
        order.setOrderDate((orderManager.getOrderDate()));

        orderDao.add(order);

        OrderSetting orderSetting = orderSettingDao.findByOrderDate((orderManager.getOrderDate()));
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);

    }

    @Override
    public void edit(OrderManager orderManager) {
        String phoneNumber = orderManager.getPhoneNumber();
        Member member = memberDao.findByTelephone(phoneNumber);
        member.setName(orderManager.getName());

        Integer id = member.getId();
        List<Order> order = orderDao.findById(id);
        for (Order order1 : order) {
            //原来的减一
            OrderSetting orderSetting = orderSettingDao.findByOrderDate(order1.getOrderDate());
            orderSetting.setReservations(orderSetting.getReservations()-1);
            orderSettingDao.editReservationsByOrderDate(orderSetting);
            //新的日期
            Date orderDate = orderManager.getOrderDate();
            //新的日期加一
            OrderSetting orderSettingNew = orderSettingDao.findByOrderDate(orderDate);
            orderSettingNew.setReservations(orderSettingNew.getReservations()+1);
            orderSettingDao.editReservationsByOrderDate(orderSettingNew);

            order1.setOrderDate(orderDate);
            order1.setOrderType(orderManager.getOrderType());
            order1.setOrderStatus(orderManager.getOrderStatus());

        }
    }

    @Override
    public Order findOrderById(Integer id) {
        return orderDao.findOrderById(id);
    }

    @Override
    public List<Integer> findSetmealIdsByOrderId(Integer id) {
        return orderDao.findSetmealIdsByOrderId(id);
    }
}
